package com.example.shoppingdemo.nslcommodity.IResult;

import android.content.Context;

import com.example.shoppingdemo.BaseApplication;
import com.example.shoppingdemo.nslcommodity.CommodityModel;
import com.example.shoppingdemo.nslcommodity.OrderForm;
import com.example.shoppingdemo.nslcommodity.javabean.AdapterModel;
import com.example.shoppingdemo.nslcommodity.javabean.CommodityDataResult;
import com.example.shoppingdemo.nslcommodity.javabean.CommodityItemList;
import com.example.shoppingdemo.nslcommodity.javabean.CommodityListResult;
import com.example.shoppingdemo.nslcommodity.javabean.CommodityResult;
import com.example.shoppingdemo.nslcommodity.javabean.ReturnsModel;
import com.example.shoppingdemo.nslcommodity.javabean.ShopModel;
import com.example.shoppingdemo.nslcommodity.orderbean.ListModel;
import com.example.shoppingdemo.nslcommodity.orderbean.OrderData;
import com.example.shoppingdemo.nslcommodity.orderbean.OrderList;
import com.example.shoppingdemo.nslcommodity.orderbean.OrderModel;
import com.example.shoppingdemo.nslcommodity.orderbean.OrderResult;
import com.example.shoppingdemo.nslcommodity.returnsbean.DemoData;
import com.example.shoppingdemo.nslcommodity.returnsbean.DemoModel;
import com.example.shoppingdemo.nslcommodity.returnsbean.DemoResult;
import com.example.shoppingdemo.nslcommodity.returnsbean.ReturnsData;
import com.example.shoppingdemo.nslcommodity.returnsbean.ReturnsList;
import com.example.shoppingdemo.nslcommodity.returnsbean.ReturnsResult;
import com.example.shoppingdemo.nslcommodity.shopbean.ShopData;
import com.example.shoppingdemo.nslcommodity.shopbean.ShopList;
import com.example.shoppingdemo.nslcommodity.shopbean.ShopResult;

import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.android.schedulers.AndroidSchedulers;
public class CommodityRs implements ICommodityResult{
    private Context mContext;
    public CommodityRs(Context mContext) {
        this.mContext = mContext;
    }
    List<AdapterModel> commodityListModels=new ArrayList<>();
    AdapterModel model;
    ShopModel shopModel;
    List<ShopModel> shopModelList=new ArrayList<>();
    List<ListModel>listModels=new ArrayList<>();
    OrderModel model1;
    ListModel listModel;
    List<CommodityModel> commodityModelList=new ArrayList<>();
    List<ReturnsModel>returnsModels=new ArrayList<>();
    DemoModel dmodel;
    @Override
    public void onCommodityresult(int id,onCommodListResponse listResponse) {
        BaseApplication.ishoppingRequest.onCommodityResult(id)
                .map(new Func1<CommodityResult, CommodityDataResult>() {
                    @Override
                    public CommodityDataResult call(CommodityResult commodityResult) {
                        return commodityResult.getData();
                    }
                }).flatMap(new Func1<CommodityDataResult, Observable<CommodityListResult>>() {
            @Override
            public Observable<CommodityListResult> call(CommodityDataResult commodityDataResult) {
                return Observable.from(commodityDataResult.getList());
            }
        }).map(new Func1<CommodityListResult, AdapterModel>() {
            @Override
            public AdapterModel call(CommodityListResult commodityListResult) {
                model=new AdapterModel();
                model.setId(commodityListResult.getId());
                model.setType(commodityListResult.getStatus());
                model.setSmoney(commodityListResult.getTotalAmount());
                model.setNumder(commodityListResult.getOrderSn());
                for (int i = 0; i <commodityListResult.getOrderItemList().length ; i++) {
                    shopModel=new ShopModel();
                    shopModel.setIcon(commodityListResult.getOrderItemList()[i].getProductPic());
                    shopModel.setMoney(commodityListResult.getOrderItemList()[i].getProductPrice());
                    shopModel.setNumder(commodityListResult.getOrderItemList()[i].getProductQuantity());
                    shopModel.setSku(commodityListResult.getOrderItemList()[i].getProductAttr());
                    shopModel.setTitle(commodityListResult.getOrderItemList()[i].getProductName());
                    shopModelList.add(shopModel);
                }
                model.setShopModelList(shopModelList);
                return model;
            }
        }).subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AdapterModel>() {
                    @Override
                    public void onCompleted() {
                        listResponse.onCommodList(commodityListModels);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(AdapterModel adapterModel) {
                        commodityListModels.add(adapterModel);
                    }
                });
    }
    public interface onCommodListResponse
    {
        void onCommodList(List<AdapterModel> commodityListModel);
    }

    @Override
    public void onOrderForm(int orderId,onOrderitemResponse inistener) {
        BaseApplication.ishoppingRequest.onOrderlistResult(orderId)
                .map(new Func1<OrderResult, OrderData>() {
                    @Override
                    public OrderData call(OrderResult orderResult) {
                        return orderResult.getData();
                    }
                }).flatMap(new Func1<OrderData, Observable<OrderList>>() {
            @Override
            public Observable<OrderList> call(OrderData orderData) {
                model1=new OrderModel();
                model1.setCouponAmount(orderData.getCouponAmount());
                model1.setCreateTime(orderData.getCreateTime());
                model1.setIntegration(orderData.getIntegration());
                model1.setIntegrationAmount(orderData.getIntegrationAmount());
                model1.setNote(orderData.getNote());
                model1.setOrderSn(orderData.getOrderSn());
                model1.setStatus(orderData.getStatus());
                model1.setPayAmount(orderData.getPayAmount());
                model1.setReceiverCity(orderData.getReceiverCity());
                model1.setReceiverDetailAddress(orderData.getReceiverDetailAddress());
                model1.setReceiverName(orderData.getReceiverName());
                model1.setReceiverPhone(orderData.getReceiverPhone());
                model1.setReceiverProvince(orderData.getReceiverProvince());
                model1.setReceiverRegion(orderData.getReceiverRegion());
                model1.setTotalAmount(orderData.getTotalAmount());
                model1.setFreightAmount(orderData.getFreightAmount());
                return Observable.from(orderData.getOrderItemList());
            }
        }).map(new Func1<OrderList, ListModel>() {
            @Override
            public ListModel call(OrderList orderList) {
                listModel=new ListModel();
                listModel.setIcon(orderList.getProductPic());
                listModel.setMoney(orderList.getProductPrice());
                listModel.setNumder(orderList.getProductQuantity());
                listModel.setTitle(orderList.getProductName());
                listModel.setSku(orderList.getProductAttr());
                listModel.setActivity(orderList.getPromotionName());
                listModel.setaMoney(orderList.getPromotionAmout());
                return listModel;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ListModel>() {
                    @Override
                    public void onCompleted() {
                        model1.setListModels(listModels);
                        inistener.onOrderitemList(model1);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(ListModel listModel) {
                        listModels.add(listModel);
                    }
                });
    }

    @Override
    public void onShopping(int id,onShopResponse shopResponse) {
        BaseApplication.ishoppingRequest.onShoppinglistResult()
                .map(new Func1<ShopResult, ShopData>() {
                    @Override
                    public ShopData call(ShopResult shopResult) {
                        return shopResult.getData();
                    }
                }).flatMap(new Func1<ShopData, Observable<ShopList>>() {
            @Override
            public Observable<ShopList> call(ShopData shopData) {
                return Observable.from(shopData.getProductList());
            }
        }).map(new Func1<ShopList, CommodityModel>() {
            @Override
            public CommodityModel call(ShopList shopList) {
                CommodityModel model=new CommodityModel();
                model.setIcon(shopList.getPic());
                model.setId(shopList.getId());
                model.setMoney(shopList.getOriginalPrice());
                model.setShop(shopList.getSubTitle());
                model.setTitle(shopList.getName());
                return model;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CommodityModel>() {
                    @Override
                    public void onCompleted() {
                        shopResponse.onShopList(commodityModelList);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(CommodityModel commodityModel) {
                        commodityModelList.add(commodityModel);
                    }
                });
    }

    @Override
    public void onReturns(CommodityRs.onReturnsResponse returnsResponse) {
        BaseApplication.ishoppingRequest.onReturnslistResult()
                .map(new Func1<ReturnsResult, ReturnsData>() {
                    @Override
                    public ReturnsData call(ReturnsResult returnsResult) {
                        return returnsResult.getData();
                    }
                }).flatMap(new Func1<ReturnsData, Observable<ReturnsList>>() {
            @Override
            public Observable<ReturnsList> call(ReturnsData returnsData) {
                return Observable.from(returnsData.getList());
            }
        }).map(new Func1<ReturnsList, ReturnsModel>() {
            @Override
            public ReturnsModel call(ReturnsList returnsList) {
                ReturnsModel model=new ReturnsModel();
                model.setIcon(returnsList.getProductPic());
                model.setId(returnsList.getId());
                model.setNumder(returnsList.getOrderSn());
                model.setTitle(returnsList.getProductName());
                model.setSku(returnsList.getProductAttr());
                model.setRefund(returnsList.getReturnAmount());
                model.setRefundType(returnsList.getStatus());
                return model;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ReturnsModel>() {
                    @Override
                    public void onCompleted() {
                        returnsResponse.onReturnsList(returnsModels);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ReturnsModel returnsModel) {
                        returnsModels.add(returnsModel);
                    }
                });
    }

    @Override
    public void onDemo(int id, onDemoResponse demoResponse) {

        BaseApplication.ishoppingRequest.onDemolistResult(id)
                .map(new Func1<DemoResult, DemoData>() {
                    @Override
                    public DemoData call(DemoResult demoResult) {
                        return demoResult.getData();
                    }
                }).map(new Func1<DemoData, DemoModel>() {
            @Override
            public DemoModel call(DemoData demoData) {
                dmodel=new DemoModel();
                dmodel.setBrand(demoData.getProductBrand());
                dmodel.setDescribe(demoData.getDescription());
                dmodel.setIcon(demoData.getProductPic());
                dmodel.setId(demoData.getProductId());
                dmodel.setMoney(demoData.getProductPrice());
                dmodel.setNumder(demoData.getProductCount());
                dmodel.setReason(demoData.getReason());
                dmodel.setSku(demoData.getProductAttr());
                dmodel.setTime(demoData.getCreateTime());
                dmodel.setTitle(demoData.getProductName());
                dmodel.setType(demoData.getStatus());
                dmodel.setzMoney(demoData.getReturnAmount());
                return dmodel;
            }
        }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<DemoModel>() {
            @Override
            public void onCompleted() {
                demoResponse.onDemoList(dmodel);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(DemoModel demoModel) {

            }
        });
    }

    public interface onOrderitemResponse
    {
        void onOrderitemList(OrderModel orderModel);
    }
    public interface onShopResponse
    {
        void onShopList(List<CommodityModel> commodityModels);
    }
    public interface onReturnsResponse
    {
        void onReturnsList(List<ReturnsModel> returnsModels);
    }
    public interface onDemoResponse
    {
        void onDemoList(DemoModel demoModel);
    }
}
